package com.moshang.blog.controller;

import com.moshang.blog.core.utils.SpringContextUtil;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * @Author xww
 * @Description //TODO
 * @Date 2019/7/15   9:18
 **/
@Component
public class TaskEndDelegate implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) throws Exception {
        ApplicationContext appCtx = SpringContextUtil.getApplicationContext();
        ProcessEngine processEngine = (ProcessEngine) appCtx.getBean("processEngine");
        RuntimeService runtimeService = (RuntimeService) appCtx.getBean("runtimeService");
/** 判断流程是否结束，查询正在执行的执行对象表 */
        ProcessInstance rpi = processEngine.getRuntimeService()
                .createProcessInstanceQuery()
                .processInstanceId(execution.getProcessInstanceId()).singleResult();
        if(rpi!=null) {
            String pdi = rpi.getProcessDefinitionId();
            String key = pdi.substring(0, pdi.indexOf(":"));
            /**proEndService 是业务审批结束处理类， 想使用rpi.getProcessDefinitionKey()进行标记，但是在此处获取时，获取为空，所以从ProcessDefinitionId中截取的**/
            /** ProcessBusinessService  是获取业务bean的工厂类**/
//            ProcessBusinessService proEndService = ProcessBusinessServiceFactory.builder(key);
            String status = (String) runtimeService.getVariable(execution.getId(), "operateApprove");
            System.out.println(rpi.getBusinessKey());
//            proEndService.endProcess(rpi.getBusinessKey(),status);
        }
    }
}
